#ifndef __PGCONN_H_
#define __PGCONN_H_

#include <crypt.h>
#include <mhash.h>
#include <syslog.h>
#include <postgresql/libpq-fe.h>
#include "pam_pg_options.h"

#define DB_FILE_CONF "/etc/pam_pg.conf"
#define DB_DEBUG 2
#define INIT_FILE_DF 0
#define INIT_FILE_USER 1
#define INIT_NO_FILE 2
#define SALT "%.2s"
#define PW_CRYPT 0
#define PW_MD5 1
#define PW_CRYPT_MD5 2
#define PW_CLEAR 3
#define PW_SHA1 4
#define PW_MD5_B64 5
#define initdb() init_db(NULL)

#define SYSLOG(x...)   openlog("pam-pg", LOG_PID, LOG_AUTH); \
                       syslog(LOG_INFO, ##x);                \
                       closelog();

typedef struct pgconn_t {
  PGconn * pgconn;
  modopt_t conf;
} * CONNECTION_DB;

CONNECTION_DB init_db(modopt_t);
void free_db(CONNECTION_DB);
int verify_password(CONNECTION_DB, const char *, const char *);
int verify_expired(CONNECTION_DB, const char *);
int verify_wheel(CONNECTION_DB, const char *);
int verify_user_on_group(CONNECTION_DB conn, const char *, const char *);
int modify_password(CONNECTION_DB, const char *, const char *);
int get_group_id(CONNECTION_DB, const char *);
int get_user_id(CONNECTION_DB, const char *);
char * get_user_login(CONNECTION_DB, const char *);
char * get_group_name(CONNECTION_DB, const char *);
//int add_user_on_group(CONNECTION_DB conn, const char *, const char *);
modopt_t load_conf(char *);
int get_op_param(char *);
int get_next_line(FILE *, char *, modopt_t);
char * encrypt_password(int, char *, char *);
char * get_crypt_salt(CONNECTION_DB, char *,int);
char * crypt_make_salt(int);
void remove_enter(char *);

#endif
